package com.kmust.dream.wanderingdream.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.product.entity.ProductImage;
import com.kmust.dream.wanderingdream.product.service.ProductImageService;
import com.kmust.dream.wanderingdream.product.mapper.ProductImageMapper;
import com.kmust.dream.wanderingdream.product.utils.ReturnUtils;
import com.kmust.dream.wanderingdream.product.vo.ProductImageBatchVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
@Transactional
public class ProductImageServiceImpl extends ServiceImpl<ProductImageMapper, ProductImage>
implements ProductImageService{

    @Override
    public ResultBean getProductImageById(Integer productId) {
        return ReturnUtils.getList(this.list(new QueryWrapper<ProductImage>().eq("product_id",productId)));
    }

    @Override
    public ResultBean addProductImage(Integer productId,String url) {
        ProductImage productImage = new ProductImage();
        productImage.setProductId(productId);
        productImage.setImageUrl(url);
        return ReturnUtils.add(this.save(productImage));
    }

    @Override
    public ResultBean updateProductImage(ProductImage productImage) {
        return ReturnUtils.update(this.updateById(productImage));
    }

    @Override
    public ResultBean deleteProductImage(Integer productImageId) {
        return ReturnUtils.delete(this.removeById(productImageId));
    }

    @Override
    public ResultBean addProductImageBatch(ProductImageBatchVO productImageBatchVO) {
        List<String> productImages = productImageBatchVO.getProductImages();
        try {
            for (String productImage : productImages) {
                ProductImage ob = new ProductImage();
                ob.setProductId(productImageBatchVO.getProductId());
                ob.setImageUrl(productImage);
                save(ob);
            }
            return ReturnUtils.add(true);
        } catch (Exception e) {
            return ReturnUtils.add(false);
        }
    }
}




